package demo2;

import java.util.List;
import java.util.Random;

public class MySingleList {



    public ListNode head; // 表示当前链表的头节点

    //创建链表
    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(45);
        ListNode node4 = new ListNode(56);
        ListNode node5 = new ListNode(76);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }


    //打印链表
    public void display() {
        ListNode cur = head;
        while (cur!=null){
            System.out.print (cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur!=null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur!=null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }


    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);


        ListNode cur = head;
        if (cur == null){  //当前链表没有元素时，head直接等于新节点
            head = node;
            return;
        }
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next = node;


    }


    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        //判断是否合法
        if (index<0 || index >size()){
            System.out.println("插入位置非法");
        }
        if (index == 0){
            node.next = head;
            head = node;
            return;
        }


        for(int i=0;i<index-1;i++){
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;


    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode pre = head;
        ListNode cur = pre.next;
        if (head.val==key){
            head = head.next;
            return;
        }
        while (pre!=null){
            if(cur.val==key){
                pre.next = cur.next;
                return;
            }
            cur = cur.next;
            pre = pre.next;
            if (cur == null){
                System.out.println("链表中没有要删除的key");
                break;
            }
        }



    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        while (head.val==key){
            head=head.next;
        }
        ListNode prev = head;
        ListNode cur = prev.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }



    }

    public void clear(){
        this.head = null;
    }





}
